#!/bin/bash
#

SELINUXS(){
echo "====selinux====";
if [[ `getenforce` == Disabled ]];then
	echo "selinux-status:close";
else 
	sed -i '/^selinux=/s|enforcing|disabled|' /etc/selinux/config &>/dev/null;
	if [[ $? -eq 0 ]];then
		echo "selinux-close:success";
	else
		echo "selinux-close:error";
	fi
fi
}

FIREWALLDS(){
echo "====firewalld====";
if `systemctl status firewalld` &>/dev/null;then
	systemctl disable firewalld.service &>/dev/null;
	systemctl stop firewalld &>/dev/null;
	if systemctl status firewalld &>/dev/null;then
		echo "firewalld-close:success";
	else
		echo "firewalld-close:error";
	fi
else
	echo "firewalld-status:close";
fi
}

YUM_DNS(){
echo "====yum_DNS====";
if systemctl status bind &>/dev/null;then
	echo "yum_DNS:ok"
else
	yum install bind -y &>/dev/null;
	if [[ $? -eq 0 ]];then
		echo "yum_DNS:success";
	else
		echo "yum_DNS:error";
	fi
fi
}

CONFS(){
echo "====conf====";
awk -F'{|}' '/listen-on port/{sub($2," any; ")};/allow/{sub($2," any; ")}' /etc/named.conf &>/dev/null;
	if [[ $? -eq 0 ]];then 
		echo "conf:success";
	else
		echo "conf:error";
	fi
}

ZONES(){
echo "====zones====";
t="true";
ip=$(ifconfig ens33 | grep '192'|awk '{print $2}')
while true;do
	read -p "请输入分区名:(XXX.com) " DNS_zones
	if grep "^zone \"${DNS_zones}\"" /etc/named.rfc1912.zones &>/dev/null;then
		echo "DNS_zones:already"
	else
		echo "zone \"$DNS_zones\" IN {
	type master;
	file \"${DNS_zones}.zone\";
};" >> /etc/named.rfc1912.zones;
		if [[ $? -eq 0 ]];then 
			echo "DNS_zone:success";
			cp -a /var/named/named.localhost /var/named/${DNS_zones}.zone &>/dev/null;
			#sed -i '2c \@       IN SOA  '$DNS_zones'. rname.invalid. (' /var/named/${DNS_zones}.zone &>/dev/null;
			#sed -i '8,$d' /var/named/${DNS_zones}.zone &>/dev/null;
			#sed -i '$a \	NS	ns01.'$DNS_zones'.' /var/named/${DNS_zones}.zone &>/dev/null;
			#sed -i '$a \ns01    A  '$ip'' /var/named/${DNS_zones}.zone &>/dev/null;
			break;
		else
			echo "DNS_zone:error";
		fi
	fi
done
}

DNS(){
echo "====DNS===="
if [[ -f "/var/named/${DNS_zones}.zone" ]];then
	sed -i '2c \@       IN SOA  '$DNS_zones'. rname.invalid. (' /var/named/${DNS_zones}.zone &>/dev/null;
   	sed -i '8,$d' /var/named/${DNS_zones}.zone &>/dev/null;
   	sed -i '$a \        NS  ns01.'$DNS_zones'.' /var/named/${DNS_zones}.zone &>/dev/null;
   	sed -i '$a \ns01    A  '$ip'' /var/named/${DNS_zones}.zone &>/dev/null;
	echo "DNS:success";
else
	echo "zone:file is not find";
	exit;
fi
}

DNS_OPEN(){
echo "====DNS_OPNE====";
if systemctl status named &>/dev/null;then
	echo "DNS_OPEN:success"
else
	systemctl enable --now named &>/dev/null;
	if [[ $? -eq 0 ]];then
		echo "DNS_OPEN:success";
	else
		echo "DNS_OPEN:error";
	fi
fi
}
#检测seinux和防火墙是否关闭
SELINUXS
FIREWALLDS


#检测系统是否安装DNS
YUM_DNS


#修改配置文件
CONFS


#创建分区
ZONES
#创建记录
DNS
#设置开机自启
DNS_OPEN
